package org.jeecg.modules.monitoring.j_monitoring.controller;

import java.util.*;
import java.util.stream.Collectors;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import org.jeecg.modules.monitoring.j_monitoring.entity.ScreenData;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.*;
import org.jeecg.modules.monitoring.j_monitoring.mapper.SoftwareSettingMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.ISoftwareSettingService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.monitoring.j_monitoring.service.IScreenDataService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 软件许可
 * @Author: xiaoyq
 * @Date: 2023-09-05
 * @Version: V1.0
 */
@Api(tags = "软件许可")
@RestController
@RequestMapping("/j_monitoring/licenseSoftware")
@Slf4j
public class LicenseSoftwareController extends JeecgController<SoftwareSetting, ISoftwareSettingService> {

    @Resource
    private IScreenDataService screenDataService;
    /**
     * 分页列表查询
     *
     * @param licenseSoftware
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "软件许可-分页列表查询")
    @ApiOperation(value = "软件许可-分页列表查询", notes = "软件许可-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SoftwareSetting>> queryPageList(SoftwareSetting licenseSoftware,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<SoftwareSetting> queryWrapper = QueryGenerator.initQueryWrapper(licenseSoftware, req.getParameterMap());
        Page<SoftwareSetting> page = new Page<SoftwareSetting>(pageNo, pageSize);
        IPage<SoftwareSetting> pageList = service.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param licenseSoftware
     * @return
     */
    @AutoLog(value = "软件许可-添加")
    @ApiOperation(value = "软件许可-添加", notes = "软件许可-添加")
    //@RequiresPermissions("org.jeecg.modules.monitoring:软件许可:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SoftwareSetting licenseSoftware) {
        service.save(licenseSoftware);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param licenseSoftware
     * @return
     */
    @AutoLog(value = "软件许可-编辑")
    @ApiOperation(value = "软件许可-编辑", notes = "软件许可-编辑")
    //@RequiresPermissions("org.jeecg.modules.monitoring:软件许可:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SoftwareSetting licenseSoftware) {
        licenseSoftware.setUpdateTime(new Date());
        service.updateById(licenseSoftware);
        return Result.OK("编辑成功!");
    }

    /**
     * 批量显示
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "软件许可-批量显示")
    @ApiOperation(value = "软件许可-批量显示", notes = "软件许可-批量显示")
    //@RequiresPermissions("org.jeecg.modules.monitoring:软件许可:deleteBatch")
    @PostMapping(value = "/showBatch")
    public Result<String> showBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        if(idList.size()==0){
            return Result.error("参数不能为空!");
        }
        UpdateWrapper<SoftwareSetting> updateWrapper = new UpdateWrapper();
        updateWrapper.lambda().set(SoftwareSetting::getIsshow,1);
        updateWrapper.lambda().set(SoftwareSetting::getUpdateTime,new Date());
        updateWrapper.lambda().in(SoftwareSetting::getId,idList);
        if(this.service.update(updateWrapper)){
            return Result.OK("批量设置成功!");
        }
        return Result.OK("批量设置失败!");
    }

    /**
     * 批量隐藏
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "软件许可-批量隐藏")
    @ApiOperation(value = "软件许可-批量隐藏", notes = "软件许可-批量隐藏")
    //@RequiresPermissions("org.jeecg.modules.monitoring:软件许可:deleteBatch")
    @PostMapping(value = "/hideBatch")
    public Result<String> hideBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        if(idList.size()==0){
            return Result.error("参数不能为空!");
        }
        UpdateWrapper<SoftwareSetting> updateWrapper = new UpdateWrapper();
        updateWrapper.lambda().set(SoftwareSetting::getIsshow,0);
        updateWrapper.lambda().set(SoftwareSetting::getUpdateTime,new Date());
        updateWrapper.lambda().in(SoftwareSetting::getId,idList);
        if(this.service.update(updateWrapper)){
            return Result.OK("批量设置成功!");
        }
        return Result.OK("批量设置失败!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "软件许可-通过id删除")
    @ApiOperation(value = "软件许可-通过id删除", notes = "软件许可-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.monitoring:软件许可:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        service.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "软件许可-批量删除")
    @ApiOperation(value = "软件许可-批量删除", notes = "软件许可-批量删除")
    //@RequiresPermissions("org.jeecg.modules.monitoring:软件许可:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "软件许可-通过id查询")
    @ApiOperation(value = "软件许可-通过id查询", notes = "软件许可-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SoftwareSetting> queryById(@RequestParam(name = "id", required = true) String id) {
        SoftwareSetting licenseSoftware = service.getById(id);
        if (licenseSoftware == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(licenseSoftware);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param licenseSoftware
     */
    //@RequiresPermissions("org.jeecg.modules.monitoring:软件许可:exportXls")
    @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
    @ApiOperation(value="软件许可-导出", notes="软件许可-导出")
    public ModelAndView exportXls(HttpServletRequest request, SoftwareSetting licenseSoftware) {
        return super.exportXls(request, licenseSoftware, SoftwareSetting.class, "软件许可");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("软件许可:importExcel")
    @ApiOperation(value="软件许可-导入", notes="软件许可-导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SoftwareSetting.class);
    }

    /**
     * 计算资源
     * @return
     */
    @RequestMapping(value = "/readComputingSettingJson", method = RequestMethod.GET)
    public Result readComputingSettingJson() {
        ScreenData screenData = screenDataService.getById("1");
        if(screenData==null){
            screenData = new ScreenData();
            screenData.setCode("1");
        }
        return Result.OK(screenData);
    }


    @RequestMapping(value = "/readStorageSettingJson", method = RequestMethod.GET)
    public Result readStorageSettingJson() {
        ScreenData screenData = screenDataService.getById("2");
        if(screenData==null){
            screenData = new ScreenData();
            screenData.setCode("2");
        }
        return Result.OK(screenData);
    }


    @RequestMapping(value = "/readBusinessSettingJson", method = RequestMethod.GET)
    public Result readBusinessSettingJson() {
        ScreenData screenData = screenDataService.getById("3");
        if(screenData==null){
            screenData = new ScreenData();
            screenData.setCode("3");
        }
        return Result.OK(screenData);
    }

    @RequestMapping(value = "/readSoftwareUserJson", method = RequestMethod.GET)
    public Result readSoftwareUserJson() {
        ScreenData screenData = screenDataService.getById("4");
        if(screenData==null){
            screenData = new ScreenData();
            screenData.setCode("4");
        }
        return Result.OK(screenData);
    }

    @RequestMapping(value = "/readCompanyInfoJson", method = RequestMethod.GET)
    public Result readCompanyInfoJson() {
        ScreenData screenData = screenDataService.getById("5");
        if(screenData==null){
            screenData = new ScreenData();
            screenData.setCode("5");
        }
        return Result.OK(screenData);
    }
}
